package com.amesit.service.privilege.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amesit.service.base.DaoSupport;
import com.amesit.service.privilege.DepartmentService;

@Service @Transactional
public class DepartmentServiceBean extends DaoSupport implements DepartmentService {

	@Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
	public boolean exist(String name) {		
		return ((Long)em.createQuery("select count(o) from Department o where o.name=?1")
		.setParameter(1, name).getSingleResult())>0;
	}

	@Override
	public <T> void delete(Class<T> entityClass, Object[] entityids) {
		StringBuffer jpql = new StringBuffer("");
		if(entityids!=null && entityids.length>0){
			for(int i=0; i<entityids.length;i++){
				jpql.append('?').append(i+2).append(',');
			}
			jpql.deleteCharAt(jpql.length()-1);
		}
		Query query = em.createQuery("update Department o set o.visible=?1 where o.id in("+ jpql.toString()+ ")");
		query.setParameter(1, false);
		for(int i=0; i<entityids.length;i++){
			query.setParameter(i+2, entityids[i]);
		}
		query.executeUpdate();
	}
	
	
}
